<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/20
 * Time: 10:24
 */
namespace app\wechat_admin\controller;

use app\wechat_admin\service\ArticleService;
use think\Controller;
use think\Request;

class Article extends Controller{
    private $per_page;

    public function __construct() {
        parent::__construct();
        $this->per_page = config('wx_config')['per_page'];
    }

    /**
     * @param Request $request
     * @return \think\response\Json
     * 获取文章列表
     */
    public function getArticleList(Request $request) {
        $where = [];
        $page = $request->post('page', '1', 'int');
        $category_id = $request->post('category_id', '', 'int');
        if (!empty($category_id)) {
            $where['category_id'] = $category_id;
        }
        $limit = ($page - 1) * $this->per_page . ',' . $this->per_page;
        $list = ArticleService::getArticleList($where, $limit);
        $total_page = ceil($list['total'] / $this->per_page);
        $next_page = ($page >= $total_page) ? $total_page : bcadd($page, 1);
        $previous_page = ($page <= 1) ? $page : bcsub($page, 1);
        $list['per_page'] = $this->per_page;
        $list['page'] = $page;
        $list['next_page'] = $next_page;
        $list['previous_page'] = $previous_page;
        $list['total_page'] = $total_page;
        return json($list);
    }

    public function showArticleDetail(Request $request) {
        $article_id = $request->get('article_id', '1', 'int');
        $where['id'] = $article_id;
        $list = ArticleService::getArticleDetail($where);
        $this->assign('list', $list);
        return $this->fetch();
    }
}